package com.raos.example;

import com.raos.example.utils.OllamaUtil;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;

/**
 * ollama聊天演示控制器
 * @author raos
 * @date 2025-02-13
 */
@Slf4j
@Mapping("/ollama")
@Controller
public class ChatOllamaController {

    @Inject("${ollama.baseUrl}")
    private String baseUrl;

    @Inject("${ollama.chat.model}")
    private String modelName;

    /**
     * 获取聊天api地址
     */
    private String getChatApiUrl() {
        String chatApi = "%s/api/generate";
        return String.format(chatApi, baseUrl);
    }

    /**
     * 初始化方法：solon容器启动完成后执行
     */
    @Init
    public void init() {
        log.info("baseUrl=[{}]", baseUrl);
        log.info("chatApiUrl=[{}]", getChatApiUrl());
        log.info("modelName=[{}]", modelName);
    }

    /**
     * 聊天内容完整回复响应
     */
    @Mapping("/aiChat")
    public String aiChat(@Param(defaultValue = "讲一个冷笑话") String message) {
        String text = OllamaUtil.generateText(modelName, message);
        log.info("text：{}", text);
        return text;
    }



}